using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Core.Common;
using Yoozoo.Gameplay.Liberty.AI;
using Yoozoo.Gta.Common;
using Yoozoo.Mars.Got;

namespace Yoozoo.Gameplay.Liberty.StreetEvent
{    
    public struct WaitRebornInfo
    {
        public int id;
        public int modelId;
        public float startRebornTime;
        public float reBornTime;
        public int dropItemPackId;
        public string rebornPoint;
    }
    public class StreetFightEvent : StreetEventBase
    {
        public static StreetFightEvent Instance;
        
        [Header("低端机Npc列表，需要预加载")]
        public List<StreetEventNpcConfig> lowQualityNpcConfigList;
        public Transform RandomWeaponPoint;
        public Transform fixWeaponPoint;
        
        [HideInInspector] public Transform[] RandomWeaponPointList;
        [HideInInspector] public Transform[] FixWeaponPointList;
        private HashSet<int> _npcStreetIdList = new HashSet<int>();
        // 奖杯道具ID
        public int ArenaCupItemId = 301;
        // 当前持有奖杯的AI
        public int HoldCupUniqueId;
        // 当前奖杯道具
        public PropItem ArenaCupPropItem;
        // 当前持有奖杯的AI计时器
        public float HoldCupTimer = 0;
        public StreetFightPropItemPoint[] PropPoints;
        private List<PropItem> allItemList = new List<PropItem>();
        private PropItem _cup;

        protected override void Awake()
        {
            // 低端机npc覆盖原始npc数据
            if ((int)QualityManager.Inst.CurrentQuality<=(int)QualityEnum.Medium && lowQualityNpcConfigList!=null && lowQualityNpcConfigList.Count > 0)
            {
                eventConfig.npcConfigList = lowQualityNpcConfigList;
            }
            
            base.Awake();
        }
        
        protected override void OnStart()
        {
            if (PropPoints == null)
            {
                PropPoints = transform.GetComponentsInChildren<StreetFightPropItemPoint>();
            }
            Instance = this;
      
            if (RandomWeaponPoint!=null)
            {
                RandomWeaponPointList = RandomWeaponPoint.GetComponentsInChildren<Transform>();
            }

            if (fixWeaponPoint!=null)
            {
                FixWeaponPointList = fixWeaponPoint.GetComponentsInChildren<Transform>();
            }
            LibertyAIUtils.PlayerController.cameraController.SetCameraOcclusionCullingActive(false);
            
            base.OnStart();
        }
        

        public override void OnEnter()
        {
            base.OnEnter();
            PropItem.OnPropItemCreate += OnPropItemCreate;
            PropItem.OnPropItemDestroy += OnPropItemDestroy;
            PropItem.OnPropItemTaken += OnPropItemTaken;
            HoldCupUniqueId = 0;
        }

        public override void OnExit()
        {
            base.OnExit();
            if (ArenaCupPropItem != null)
            {
                PropItem.DestroyPropItem(ArenaCupPropItem);
                ArenaCupPropItem = null;
            }
            PropItem.OnPropItemCreate -= OnPropItemCreate;
            PropItem.OnPropItemDestroy -= OnPropItemDestroy;
            PropItem.OnPropItemTaken -= OnPropItemTaken;
            allItemList.Clear();
            allItemList = null;
        }

        protected override void OnUpdate()
        {
            if (HoldCupUniqueId != 0)
            {
                HoldCupTimer += Time.deltaTime;
            }
        }
        public void SetCup(PropItem cup)
        {
            _cup = cup;
        }

        public Transform GetCup()
        {
            if (_cup!=null)
            {
                return _cup.transform;;
            }
            return null;
        }

        public List<PropItem> GetAllItemTrans()
        {
            allItemList.Clear();
            foreach (var prop in PropPoints)
            {
                prop.GetAllItem(allItemList);
            }

            if (AddedItemList!=null)
            {
                foreach (var item in AddedItemList)
                {
                    allItemList.Add(item);
                }
            }
            return allItemList;
        }
        
        public Transform GetAItemTrans(int itemId)
        {
            Transform transform;
            foreach (var point in PropPoints)
            {
                var result = point.GetAItemById(itemId);
                if (result!=null)
                {
                    transform = result;
                    break;
                }
            }
            return null;
        }

        public void GetAllItemTransArrayById(int itemId,List<PropItem> list)
        {
            foreach (var point in PropPoints)
            {
                point.GetAllItemById(itemId,list);
            }
        }
        public void GetAllItemTransArrayByBindId(int bindId,List<PropItem> list)
        {
            foreach (var point in PropPoints)
            {
                point.GetAllItemByBindId(bindId,list);
            }
        }
        public void GetAllItemTransArrayByType(int type,List<PropItem> list)
        {
            foreach (var point in PropPoints)
            {
                point.GetAllItemByType(type,list);
            }
        }

        void OnPropItemCreate(PropItem propItem)
        {
            if (propItem.ItemId == ArenaCupItemId)
            {
                ArenaCupPropItem = propItem;
            }
        }

        void OnPropItemDestroy(PropItem propItem)
        {
            if (propItem.ItemId == ArenaCupItemId)
            {
                ArenaCupPropItem = null;
            }
        }
        
        void OnPropItemTaken(int itemId, int uniqueId,long Uid,PropItem propItem)
        {
            if (_cup!=null && _cup.UID == uniqueId && itemId == ArenaCupItemId)
            {
                _cup = null;
            }

            if (StreetEventOnPropItemTakenEvent.OnPropItemTaken!=null)
            {
                // 道具拾取回调
                StreetEventOnPropItemTakenEvent.OnPropItemTaken(itemId, uniqueId,propItem);
            }
        }

        protected override void OnKillMonster(int uniqueId,int killerUniqueId,int weaponId)
        {
            base.OnKillMonster(uniqueId,killerUniqueId,weaponId);
       
            ResidentHandler.Inst.GetFunction("CityLuaFunction.OnKillInfoFresh").Action(uniqueId,killerUniqueId,weaponId);
        }

        public void AddStreetId(int streetNpcId)
        {
            _npcStreetIdList.Add(streetNpcId);
        }

        public HashSet<int> GetAllStreetNpcId()
        {
            return _npcStreetIdList;
        }
    }
}
